Android系统中操作数据库最基本的类是SQLiteDatabase,该类提供了一系列方法来打开、关闭和管理数据库,使用该类我们可以很方便地建立数据库,然后对数据库进行增、删、改、查等操作。

下面列出该类的主要方法。

1、创建、打开、关闭和删除数据库

SQLiteDatabase提供了一些OpenXXX函数来打开数据库,提供了Close来关闭数据库。

  • static SQLiteDatabase create(SQLiteDatabase.CursorFactory factory):创建内存数据库,当数据库被关闭时删除该数据库。

  • static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags, DatabaseErrorHandler errorHandler):打开数据库。

  • static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags):打开数据库

  • static SQLiteDatabase openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler):等同于openDatabase(path, factory, CREATE_IF_NECESSARY, errorHandler),如果数据库不存在则创建。

  • static SQLiteDatabase openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory):等同于openDatabase(path, factory, CREATE_IF_NECESSARY),如果数据库不存在则创建。

  • static SQLiteDatabase openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory factory):等同于openDatabase(file.getPath(), factory, CREATE_IF_NECESSARY),如果数据库不存在则创建。

  • static boolean deleteDatabase(File file):删除数据库

  • final StringgetPath():数据库路径。

  • intgetVersion():数据库版本。

  • booleanisOpen():数据库是否打开

  • boolean isReadOnly():数据库是否只读。

在实际使用中,首先使用openOrCreateDatabase(path,factory)来创建或打开数据库。

  SQLiteDatabase db=SQLiteDatabase.openOrCreateDatabase(“mydb”,null);

2、操作数据库

在SQLiteDatabase中,对数据库进行增、删、改、查,既可以使用SQL语句进行,也可以使用SQLiteDatabase提供的方法进行,两者有利有弊。

执行SQL语句的方法包括:

  • void execSQL(String sql):执行一条SQL语句。

  • void execSQL(String sql, Object[] bindArgs):执行一条SQL语句。该方法和上面的execSQL(String sql)区别在于前者支持占位符。注意这两个方法均不返回值,因此如果执行Select等需要返回结果集的SQL语句,不能使用该方法,应该使用下面的rawQuery()方法。

  • Cursor rawQuery(String sql, String[] selectionArgs):执行SQL语句并返回结果集。

下面简单介绍一下占位符,所谓“占位符“指的是在SQL语句中使用“?”来表示该位置为一个参数,需要用实际参数来替换它。例如我们想要执行删除语句,一种写法如下:

delete from table_name where id=’1’

其中’1’是删除条件。如果每次删除时条件不同,则我们必须每次生成不同的字符串,比较繁琐,一种生成SQL语句并执行该SQL的写法如下:

String strSQL=“delete from table_name where id=’”+idvalue+”’”;  
execSQL(strSQL);

更加简洁地写法是使用占位符:

execSQL(“delete from table_name where id=?”,new String[]{idvalue });

可以看出使用占位符使得编码更加简洁,并且有效避免了参数中包括单引号等情况。建议读者在执行有参数的SQL语句时使用占位符写法。

SQLiteDatabase提供的操作方法包括:

  • long insert(String table, String nullColumnHack, ContentValues values):向数据库指定表中插入数据,其中table为表的名字,values为插入的数据。

  • int update(String table, ContentValues values, String whereClause, String[] whereArgs):更新数据库指定表中的数据,其中table为表的名字,values为更新的值,whereClause为更新条件,whereArgs为更新条件中占位符所对应的实际参数值。

  • int delete(String table, String whereClause, String[] whereArgs):删除数据库指定表中的数据。其中table为表的名字,whereClause为删除条件,whereArgs为删除条件中占位符所对应的实际参数值。

  • Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit):查询数据库指定表中数据,并返回结果集。

  • Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, CancellationSignal cancellationSignal):查询数据库指定表中数据,并返回结果集。

  • Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy):查询数据库指定表中数据,并返回结果集。

  • Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit):查询数据库指定表中数据,并返回结果集。

3、事务

Android提供了SQLiteDatabase来访问数据库,该类功能强大,包括了创建、删除、执行SQL语句等数据库管理功能。同时也提供了更方便使用的SQLiteOpenHelper。

  • void beginTransaction():开始事务。

  • void beginTransactionNonExclusive():开始事务

  • void beginTransactionWithListener(SQLiteTransactionListener transactionListener):开始事务

  • void beginTransactionWithListenerNonExclusive(SQLiteTransactionListener transactionListener):开始事务

  • boolean inTransaction():是否位于事务中

  • void endTransaction():结束事务。

4、Cursor

在rawQuery()和query()方法中均返回Cursor游标对象。通过该对象可以访问结果集中数据。可以将结果集简单地理解为一个表,该表有多行,每行对应一条记录,每条记录有多个列,每个列有自己的列名和索引值,其中索引值均是从0开始的,游标就好象一个指针,指向了表中的一行。

Cursor类对于列和行等提供了以下方法:

  • intgetColumnCount():得到列数。

  • intgetColumnIndex(String columnName):得到列(列名为columnName)的基于0开始的索引值,如果不存在列名为columnName的列,则返回-1。

  • StringgetColumnName(int columnIndex):得到指定索引值的列名。

  • String[]getColumnNames():返回所有列名。

  • intgetCount():返回结果集行数。

Cursor类中提供了如下方法得到某列的值或类型:

  • XXXgetXXX(int columnIndex):得到指定列具体值,其中XXX为类型,取值为Short、String、Int、Float、Long等。

  • intgetType(int columnIndex):得到某列的值类型。

  • boolean isNull(int columnIndex):指定列是否为null。

如何通过Cursor来获得在结果集中的位置、移动位置呢?Cursor提供了下列方法:

  • boolean isFirst():是否位于第一行

  • boolean isLast():是否位于最后一行

  • boolean moveToPosition(int position):移动到第position行。position是相对结果集第一行而言的。

  • boolean move(int offset):相对于当前位置移动offset行。如果offset为正,则向下(后)移动,否则向上(前)移动。

  • boolean moveToFirst():移动到第一行

  • boolean moveToLast():移动到最后一行。

  • l boolean moveToNext():向后移动一行。

  • boolean moveToPrevious():向前移动一行。 e

results matching ""

    No results matching ""